package com.eagle.common.swagger.configuration;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicates;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Project: empire <br>
 *
 * @author <a href="836327318@qq.com">abc</a>
 * @date Created in 2020/5/21 15:19 <br>
 */

@Getter
@Setter
@Configuration
@EnableKnife4j
@EnableSwagger2
@Import(BeanValidatorPluginsConfiguration.class)
@ConfigurationProperties(prefix = "swagger")
public class SwaggerConfiguration {

    private String title;

    private String contact;

    private String version;

    private String license;

    private String licenseUrl;

    private String termsOfServiceUrl;

    private String description;

    private String basePackage;

    private List<Header> headers;

    private Boolean enabled = false;

    /**
     * 创建RestApi 并包扫描controller
     *
     * @return
     */
    @Bean
    public Docket createRestApi() {

        final Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enabled)
                .select()
                // // 对所有api进行监控
                // .apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                //不显示错误的接口地址
                .paths(PathSelectors.any())
                //错误路径不监控
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

        final List<Header> nonNullheaders = Optional.ofNullable(this.headers).orElse(new ArrayList<>());
        for (Header header : nonNullheaders) {
            ParameterBuilder ticketPar = new ParameterBuilder();
            List<Parameter> pars = new ArrayList<>();
            ticketPar
                    .name(header.getName())
                    .description(header.getDesc())
                    .modelRef(new ModelRef("string"))
                    .parameterType("header")
                    //header中的ticket参数非必填，传空也可以
                    .required(false).build();
            pars.add(ticketPar.build());

            docket.globalOperationParameters(pars);
        }

        return docket;
    }

    /**
     * 创建Swagger页面 信息
     *
     * @return
     */
    private ApiInfo apiInfo() {
        Contact contactObj = new Contact(contact, "", "");

        return new ApiInfoBuilder().
                title(title).
                contact(contactObj).
                version(version).
                license(license).
                licenseUrl(licenseUrl).
                termsOfServiceUrl(termsOfServiceUrl).
                description(description)
                .build();
    }

    @Data
    public static class Header {
        private String name;
        private String desc;
    }
}

